package com.kehao.leetcode.hard.tree_graph;

import com.kehao.utils.LeetCodeUtil;
import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

public class AroundAreaSolution {
    public void solve(char[][] board) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if(board[i][j]=='O'){
                    HashSet<Integer> set = new HashSet<>();
                    addToAccessSet(i,j,board,set);
                    deal(set,board);
                }
            }
        }
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if(board[i][j]=='o'){
                    board[i][j]='O';
                }
            }
        }
    }

    private void deal(HashSet<Integer> set, char[][] board) {
        char result = 'X';
        for(int num :set){
            int i = num/200;
            int j = num%200;
            if(i==0||i==board.length-1||j==0||j== board[0].length-1){
                result = 'o';
                break;
            }
        }
        for(int num :set){
            int i = num/200;
            int j = num%200;
            board[i][j] = result;
        }

    }

    private void addToAccessSet(int i, int j, char[][] board,HashSet<Integer> set) {
        if(set.contains(i*200+j)) return ;
        set.add(i*200+j);
        if(i-1>=0&&board[i-1][j]=='O')addToAccessSet(i-1,j,board,set);
        if(i+1<board.length&&board[i+1][j]=='O')addToAccessSet(i+1,j,board,set);
        if(j-1>=0&&board[i][j-1]=='O')addToAccessSet(i,j-1,board,set);
        if(j+1<board[0].length&&board[i][j+1]=='O')addToAccessSet(i,j+1,board,set);
    }

    @Test
    public void test(){
        String originStr = "[[\"O\",\"X\",\"O\",\"O\",\"X\",\"X\"],[\"O\",\"X\",\"X\",\"X\",\"O\",\"X\"],[\"X\",\"O\",\"O\",\"X\",\"O\",\"O\"],[\"X\",\"O\",\"X\",\"X\",\"X\",\"X\"],[\"O\",\"O\",\"X\",\"O\",\"X\",\"X\"],[\"X\",\"X\",\"O\",\"O\",\"O\",\"O\"]]";
        String result = LeetCodeUtil.charTransform(originStr);
        result = LeetCodeUtil.bracketTransform(result);
        System.out.println(result);
    }

    @Test
    public void test01(){
        char[][]board ={{'X','X','X','X'},{'X','O','O','X'},{'X','X','O','X'},{'X','O','X','X'}};
        output(board);
        solve(board);
        System.out.println("-------------");
        output(board);
    }

    @Test
    public void test02(){
        char[][]board ={{'O','X','O','O','X','X'},{'O','X','X','X','O','X'},{'X','O','O','X','O','O'},{'X','O','X','X','X','X'},{'O','O','X','O','X','X'},{'X','X','O','O','O','O'}}
                ;
        output(board);
        solve(board);
        System.out.println("-------------");
        output(board);
    }

    private void output(char[][]board){
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                System.out.print(board[i][j]+" ");
            }
            System.out.println();
        }
    }
}
